﻿namespace BEGDC.CC.Web.UserControls.ESC
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Collections;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;
	using BEGDC.CC.BL.CCFlow.ESC;
	using BEGDC.CC.Entity;
	using BEGDC.CC.BL.Workflow;
	using BEGDC.CC.BL.Common;
	using BEGDC.Utility.Castle;
	using BEGDC.CC.BL.Common.Query;

	/// <summary>
	///		Summary description for CloseApp.
	/// </summary>
	public class CloseApp : BaseESC
	{
		#region Variables
		protected System.Web.UI.WebControls.Label lbAppNo;
		protected BEGDC.CC.Web.UserControls.ESC.ESCCtrl ESCCtrl;
		protected System.Web.UI.WebControls.Button Btn_Submit;
		protected System.Web.UI.WebControls.Button Btn_Save;
		//protected System.Web.UI.WebControls.TextBox Ctl_EZVisitID;
		//protected System.Web.UI.WebControls.TextBox Ctl_LegalID;
		//protected System.Web.UI.WebControls.TextBox Ctl_Comment;
		protected System.Web.UI.HtmlControls.HtmlTableRow TR_EZVISID;
        protected System.Web.UI.HtmlControls.HtmlTableRow TR_LegalID;
		protected System.Web.UI.WebControls.TextBox tbOperationFlag;
		protected System.Web.UI.WebControls.TextBox tbOperationContent;
		protected System.Web.UI.WebControls.TextBox tbComment;
		protected System.Web.UI.WebControls.Button Btn_Query;
		protected BEGDC.CC.Web.Common.StampCtrl StampCtrl1;
		protected System.Web.UI.WebControls.DataGrid dgCostCenter;
		protected BEGDC.CC.Web.UserControls.CV.CVPrintCtrl cvPrintCtrl;
        protected System.Web.UI.WebControls.Button btnClose;
		#endregion

		private void Page_Load(object sender, System.EventArgs e)
		{
			if(!this.IsPostBack)
			{
				Session[SessionFlag.CostCenter] = null;
				base.PreInitForm();

				this.Btn_Save.Attributes["onclick"] = "javascript:OnSave();";
				this.Btn_Submit.Attributes["onclick"] = "javascript:OnSubmit();";
				this.Btn_Query.Attributes.Add("onclick", "javascript:OpenQueryPane("+Request.QueryString["AppID"]+");");
                this.btnClose.Attributes.Add("onclick", "javascript:SimpleCloseWindow();");

				InitForm();

				this.ESCCtrl.SetCloseControlVisiable();
				this.ESCCtrl.BindCloseControl();
				this.ESCCtrl.SetCloseControlEnable();
			}
			ButtonStatusSetting();
		}

		#region ControlStatusSetting
		private void ControlStatusSetting(Accessibility access )
		{
			if( access == Accessibility.Readonly )
			{
				this.DisableChildrenControls(this);
				//this.stampCtrl1.Visible = false;
			}
		}
		#endregion

		#region InitForm
		private void InitForm()
		{
			bool isReadOnly = true;
			long flowId  = this.LoadApplication();

			if(this.Application!=null)
			{
				if(this.Application.AppNo != string.Empty)
					this.lbAppNo.Text = "( "+this.Application.AppNo+" )";

				ESCCtrl.isCoveredPerson = !this.Application.IsTransition;
				ESCCtrl.isReadOnly = isReadOnly;
				ESCCtrl.DisableEditESC();
			}
			else
			{
				WebUtility.Go2ErrorPage( this.Page, "wrong Application ID" );
			}
		}
		#endregion

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.Btn_Query.Click += new EventHandler(this.btnQuery_Click);
			this.Btn_Save.Click += new EventHandler(this.Btn_Save_Click);
			this.Btn_Submit.Click += new EventHandler(this.Btn_Submit_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		#region Save
		private void Btn_Save_Click(object sender, EventArgs e)
		{
			try
			{
//				IESC ESCService = AppContainer.Instance[ServiceConst.ESCService] as IESC;
//				BEGDC.CC.Entity.Application App = ESCService.GetApplication(this.AppID);
//
//				App.EZVisitReqNo = this.Ctl_EZVisitID.Text;
//				App.LGDNo = this.Ctl_LegalID.Text;
//				App.Summary = this.Ctl_Comment.Text;
//
//				ESCService.SaveEZCloseApp(App);
				this.ESCCtrl.SaveCloseControl();

				Page.RegisterClientScriptBlock("","<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CM001 + "');</script>");
			}
			catch(Exception exp)
			{
				Page.RegisterClientScriptBlock("","<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CR002 + "');</script>");
			}
		}
		#endregion

		#region Submit
        private void Btn_Submit_Click(object sender, EventArgs e)
		{
			try
			{
				this.ESCCtrl.SubmitCloseControl();

//				IESC ESCService = AppContainer.Instance[ServiceConst.ESCService] as IESC;
//				BEGDC.CC.Entity.Application App = ESCService.GetApplication(this.AppID);
//
//				if(this.Ctl_EZVisitID.Text!=""&&this.Ctl_LegalID.Text!=""&&this.Ctl_Comment.Text!="")
//				{
//					App.EZVisitReqNo = this.Ctl_EZVisitID.Text;
//					App.LGDNo = this.Ctl_LegalID.Text;
//					App.Summary = this.Ctl_Comment.Text;
//
//					ESCService.SubmitEZCloseApp(App, true, this.tbComment.Text);
//
//					Page.RegisterClientScriptBlock("","<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CM001 + "');window.close();</script>");
//				}
//				else
//				{
//					Page.RegisterClientScriptBlock("","<script>alert('Please fill in EZVisit ID, Legal ID and Knowledge Sharing');</script>");
//					return;
//				}
				Page.RegisterClientScriptBlock("","<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CM001 + "');window.close();</script>");
			}
			catch(Exception exp)
			{
				Page.RegisterClientScriptBlock("","<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CR002 + "');window.close();</script>");
			}
		}
		#endregion

		#region Export
        private void Btn_Export_Click(object sender, EventArgs e)
		{
			try
			{
				ExcelTool.ICCExcelExport excelExport;
				if (this.Application.IsTransition)
					excelExport=new ExcelTool.ExportCRFormA();
				else
					excelExport=new ExcelTool.ExportCRFormB();
				excelExport.Export(this.Page, this.AppID );
				Page.RegisterClientScriptBlock("","<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CM001 + "');</script>");

			}
			catch (BEGDC.Utility.ExceptionManagement.GDCException exp)
			{
				Page.RegisterClientScriptBlock("","<script>alert('" + BEGDC.CC.Resource.ErrorMessage.Export_0001 + "');</script>");
			}
		}
		#endregion

		#region Query
        private void btnQuery_Click(object sender, EventArgs e)
		{
			try
			{
				if(this.tbOperationFlag.Text == "query")
				{
					IESC ESCService = AppContainer.Instance[ServiceConst.ESCService] as IESC;
					ESCService.Query(Convert.ToInt64(Request.QueryString["FlowID"]), this.tbOperationContent.Text, Session["Comment"].ToString());
				}
				Page.RegisterClientScriptBlock("","<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CM001 + "');window.close();</script>");
			}
			catch
			{
				Page.RegisterClientScriptBlock("","<script>alert('" + BEGDC.CC.Resource.SystemMessage.CR002 + "');window.close();</script>");
			}
		}
		#endregion

		#region ButtonStatusSetting
		protected override void ButtonStatusSetting()
		{			
			if( this.Access == null )
				return;

			this.Btn_Save.Visible = this.Access.IsButtonVisible(ESCTransitionList.Save);
			this.Btn_Submit.Visible = this.Access.IsButtonVisible(ESCTransitionList.Close);
			this.Btn_Query.Visible = this.Access.IsButtonVisible(ESCTransitionList.Query);
			this.cvPrintCtrl.ProcessType = WorkflowTypes.ESC;
			this.cvPrintCtrl.Visible = this.Access.IsButtonVisible(ESCTransitionList.Copy);
		}
		#endregion
	}
}
